package cn.tedu.rebbimq.m4_routting;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.Scanner;
import java.util.UUID;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setUsername("admin");
        f.setPassword("admin");
        f.setVirtualHost("/ryan");
        Channel c = f.newConnection().createChannel();

        //定义队列.定义交换机.绑定
        String queue = UUID.randomUUID().toString();
        c.queueDeclare(queue,false,true,true,null);
        c.exchangeDeclare("jhj_2","direct");

        //输入绑定键进行绑定
        System.out.println("输入绑定键,用空格隔开: ");
        String s = new Scanner(System.in).nextLine();
        String[] a = s.split("\\s+");
        for(String key : a){
            c.queueBind(queue,"jhj_2",key);
        }



        DeliverCallback deliverCallback = new DeliverCallback() {
            @Override
            public void handle(String s, Delivery delivery) throws IOException {
                String msg = new String(delivery.getBody());
                String key = delivery.getEnvelope().getRoutingKey();
                System.out.println("收到[ "+key + " ]的信息: "+msg);
            }
        };
     CancelCallback cancelCallback =   new CancelCallback() {
            @Override
            public void handle(String s) throws IOException {

            }
        };

        //消费数据
        c.basicConsume(queue,true, deliverCallback, cancelCallback);

    }
}
